<?php



function BuildDDList($nome_especie){
  $ddListItems=_BuildDDListItems("ontologia", "0", "0", null, $nome_especie);
  return "<select name='nome_especie' id='nome_especie'><option value='pai' class='node'>Ontologia das Plantas</option>$ddListItems</select>";
}

function _BuildDDListItems($table, $parent, $level, $db_cnn, $especie){
  $indent = "";
  if(!$db_cnn){$db_cnn=_connectDB();} 

  for ($i=0; $i<$level; $i++){
    $indent.= _DummySpaces(4); 
  }
  
  $iSQL="
  Select
    `lp`.`id`,`lp`.`pai`,`lp`.`planta`,
    ( Select 
        Count(*)
      From
        `$table` As `clp` Where `clp`.`pai`=`lp`.`id`
    ) As `num_sub_nodes` 
  From
    `$table` As `lp`
  Where
    `lp`.`pai` =  '$parent'
  ";
  $sql = mysql_query($iSQL,$db_cnn) or die (mysql_error());
  while ($myrow = mysql_fetch_array($sql)){
    
    if (_IsNode($myrow["num_sub_nodes"],$myrow["pai"])){
      $prefix="";
      $class='class="node"';
    }
    else{
      $prefix="-"._DummySpaces(1);
    }
    
    if(@$class == 'class="node"'){
    	$value='pai';
    }
    else{
    	$value=$myrow['planta'];
    }
    
    if($especie == $myrow['planta']){
    	$selected = 'selected';
    }
    else{
    	$selected = '';
    }
    
    @$items.="<option $selected value='$value' $class>$indent$prefix$myrow[planta]</option>";
    
    $items.=_BuildDDListItems($table, $myrow["id"], $level+1,$db_cnn,$especie);
  }
  return @$items;
}


function _IsNode($num_nodes,$parent){ return ($num_nodes>0 || $parent==0);}


function _DummySpaces($num){
  for ($i=0;$i<$num;$i++) @$dummy.="&nbsp;";
  return $dummy;
}


function _connectDB(){
  $db = mysql_connect("localhost", "apm", "apm");
  mysql_select_db("projetoapm",$db) or die("Impossivel Conectar ao Banco");
  return $db;
}

?>